﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.RateLimiting;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Query;
using System.Linq.Expressions;
using Yz.Base;
using Yz.Core.Extensions;
using Yz.DataAccess.DbAccessses.Interface;
using Yz.DB.EFCore.Dal;
using Yz.Model.Constants;
using Yz.Model.Entities;
using Yz.Model.Views;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace Yz.ApiService.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    [EnableRateLimiting("Yz.Net")]
    public class ValuesController : BaseApiController
    {
        private readonly IDbAccess<User> _userDbAccess;
        private readonly IUserAccess _userAccess;
        private readonly IYzInitializer _yzInitializer;
        public ValuesController(
            IDbAccess<User> userDbAccess,
            IYzInitializer yzInitializer,
            IUserAccess userAccess)
        {
            _yzInitializer = yzInitializer;
            _userDbAccess = userDbAccess;
            _userAccess = userAccess;
        }
        // GET: api/<ValuesController1>
        [HttpGet]
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET api/<ValuesController1>/5
        [HttpGet("{id}")]
        public async Task<string> Get(int id)
        {
            int count = await _userAccess.SaveUserAsync(new User());
            return "true";
        }

        // POST api/<ValuesController1>
        [HttpPost]
        public void Post([FromBody] string value)
        {
        }
        [Route("list")]
        public IActionResult GetList()
        {
            var userList = new List<VmUser>();
            for (int i = 0; i < 10; i++)
            {
                userList.Add(new VmUser()
                {
                    Id = i,
                    LoginName = "H啊哈" + i,
                    UpdateTime = DateTime.Now
                });
            }
            var strUser = userList.ToJsonStr();

            var uList = strUser.ToJsonObject<List<VmUser>>();
            _userDbAccess.Delete(p => p.Id > 100);
            Expression<Func<SetPropertyCalls<User>, SetPropertyCalls<User>>> setPropertyCalls = p => p.SetProperty(p => p.LoginPwd, "").SetProperty(p => p.LoginName, "");
            //  _userAccess.Update(setPropertyCalls, p => p.Id > 100);
            return new JsonResult(userList);

        }
        [Route("list1")]
        public async Task<IActionResult> GetList1()
        {
            var userList = new List<User>();
            string defaultPwd = DefaultConstant.Password.ToMD5Encrypt();
            for (int i = 0; i < 10; i++)
            {
                userList.Add(new User()
                {
                    Id = i,
                    LoginName = "H啊哈" + i,
                    UpdateTime = DateTime.Now,
                    StatusType = StatusType.Enable,
                    UserType = Model.Enums.UserType.Student,
                    LoginPwd = defaultPwd
                });
            }
            //await _adminDbAccess.BatchAddAsync(userList);
            // _adminAccess.Delete(p => p.Id > 100);
            //  Expression<Func<SetPropertyCalls<Admin>, SetPropertyCalls<Admin>>> setPropertyCalls = p => p.SetProperty(p => p.LoginPwd, "").SetProperty(p => p.LoginName, "");
            //  _userAccess.Update(setPropertyCalls, p => p.Id > 100);
            var iqueryable = _userDbAccess.GetQueryable(p => p.Id < 1000);
            var list = await iqueryable.Take(10).ToListAsync();

            return Ok(list);

        }
        [Route("list2")]
        public async Task<IActionResult> GetList2()
        {
            var userList = new List<VmUser>();
            for (int i = 0; i < 10; i++)
            {
                userList.Add(new VmUser()
                {
                    Id = i,
                    LoginName = "H啊哈" + i,
                    UpdateTime = DateTime.Now
                });
            }
            await _yzInitializer.InitDataAsync();
            object uList = userList.Select(p => new { p.LoginName, p.UpdateTime, p.Id, p.StatusType }).ToList();
            return Ok(uList.ToVmList());

        }
        // PUT api/<ValuesController1>/5
        [HttpPut("{id}")]
        public void Put(int id, [FromBody] string value)
        {

        }

        // DELETE api/<ValuesController1>/5
        [HttpDelete("{id}")]
        public void Delete(int id)
        {
        }
    }
}
